
package com.ukao.pad.presenter;

import android.content.Context;
import android.widget.RadioButton;

import com.ukao.pad.R;
import com.ukao.pad.base.BasePresenter;
import com.ukao.pad.bean.FactoryBatchInBean;
import com.ukao.pad.bean.ProductBatchInBean;
import com.ukao.pad.bean.StringBean;
import com.ukao.pad.consts.Constant;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.retrofit.ApiCallback;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.Utils;
import com.ukao.pad.view.StartFactoryView;

import java.util.HashMap;

import info.hoang8f.android.segmented.SegmentedGroup;
import rx.Observable;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class StartFactoryPesenter extends BasePresenter<StartFactoryView> {


    private    int  enterType=0;
    public StartFactoryPesenter(StartFactoryView view) {
        attachView(view);
    }

    /**
     * 工厂接收   - 扫码查询
     *
     * @param context
     * @param keywords   搜索内容
     *
     */
    public void receiveFactory(Context context, String keywords, SegmentedGroup segmentedGroup) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        Observable mObservable = null;
          //.1批次入厂 2.订单入厂  3衣物入厂
        switch (segmentedGroup.getCheckedRadioButtonId()){
            case R.id.radio_button1 :
                enterType=1;
               mHashMap.put( "batchCode", keywords);
                mHashMap.put("accessToken", SaveParamets.getToken(context));
                mObservable = apiStores.receiveFactory(Constant.createParameter(mHashMap));
                break;
            case R.id.radio_button2 :
                enterType=2;
                mHashMap.put( "keywords" , keywords);
                mHashMap.put("accessToken", SaveParamets.getToken(context));
                mObservable = apiStores.orderFactoryIn(Constant.createParameter(mHashMap));

                break;
            case R.id.radio_button3 :
                enterType=3;
                mHashMap.put( "scanCode" , keywords);
                mHashMap.put("accessToken", SaveParamets.getToken(context));
                mObservable = apiStores.prodetail(Constant.createParameter(mHashMap));
                break;
        }


        addSubscription(mObservable,
                new ApiCallback<FactoryBatchInBean>() {
                    @Override
                    public void onSuccess(FactoryBatchInBean model) {

                        if (model.getHttpCode() == 200) {


                            model.getData().setEnterFactoryType(enterType);
                            mvpView.loadbatchSuccess(model.getData());

                        } else {
                            mvpView.loadbatchFile(model.getMsg());

                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }

    /**
     * 右边衣物列表
     *
     * @param batchInId 批次id /订单id

     */
    public void productBatchInlist(String batchInId,FactoryBatchInBean  mFactoryBatchInBean) {

        int isFactoryOrder = mFactoryBatchInBean.getEnterFactoryType();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put(isFactoryOrder==2 ? "orderId" : "inId", batchInId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        Observable mObservable;
        if (isFactoryOrder==2) {
            mObservable = apiStores.orderProductRelList(Constant.createParameter(mHashMap));
        } else {
            mObservable = apiStores.orderProductBatchIn(Constant.createParameter(mHashMap));
        }
        addSubscription(mObservable,
                new ApiCallback<ProductBatchInBean>() {
                    @Override
                    public void onSuccess(ProductBatchInBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.loadbatchListSuccess(model.getData());

                        } else {
                            mvpView.loadFail(model.getMsg());

                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.refreshFinish();
                    }
                });
    }


    /*入厂上报缺失　参数*/
    public void loseFactory(Context context, String id, String inId) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("ids", id);
        mHashMap.put("inId", inId);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.loseFactory(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.loseFactorySuccess(model.getMsg());

                        } else {
                            mvpView.loadFail(model.getMsg());

                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }


    /**
     * 確定入厂
     * @param id
     */
    public void sureConfirmFactory(String id,int   isOrder ) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id", id);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        Observable observable = null;
        if (isOrder==2) {
            observable = apiStores.orderInconfirm(Constant.createParameter(mHashMap));
        } else {
            observable = apiStores.sureConfirmFactory(Constant.createParameter(mHashMap));
        }
        addSubscription(observable,
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.sureFactorySuccess();

                        } else {
                            mvpView.loadFail(model.getMsg());

                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }

    /**
     * 入厂已全部核对
     * @param id
     */
    public void allCompleteCheck( String id,int  intEnterFactoryType) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put(intEnterFactoryType==2?"orderId":"inId", id);//入库批次id
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        Observable observable;
        if (intEnterFactoryType==2){
            observable=apiStores.clothingInConfirm(Constant.createParameter(mHashMap));
        }else{
            observable=apiStores.enterFactoryCompleteCheck(Constant.createParameter(mHashMap));
        }

        addSubscription(observable,
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.checkAllFactory(model.getMsg());

                        } else {
                            mvpView.loadFail(model.getMsg());

                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }


    /**
     * 修改入厂操作类型
     *
     * @param inType 1.批次入厂 2.订单入厂
     */
    public void updateInType(final int inType, final RadioButton mRadioButton) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("inType", String.valueOf(inType));
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.updateInType(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            SaveParamets.setOrderEnter(inType);

                        } else {
                            mvpView.updateInTypeFail(mRadioButton);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mvpView.updateInTypeFail(mRadioButton);
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }


}
